package com.situ.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.ssm.entity.Department;
import com.situ.ssm.entity.Role;
import com.situ.ssm.entity.User;
import com.situ.ssm.entity.UserSearchBO;
import com.situ.ssm.service.IUserService;
import com.situ.ssm.util.JSONResult;


@Controller
@RequestMapping("/users")
public class UserController {
	@Autowired
	private IUserService userService;
	
	@ResponseBody
	@RequestMapping("/selectAll")
	public JSONResult selectAll(Integer page, Integer limit, UserSearchBO userSearchBO) {
		JSONResult jsonResult = userService.selectAll(page, limit, userSearchBO);
		return jsonResult;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete")
	public JSONResult delete(Integer id){
		System.out.println(id);
		boolean isSuccess = userService.delete(id);
		System.out.println(isSuccess);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
		return jsonResult;
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteAll")
	private JSONResult deleteAll(@RequestParam(value = "ids")String ids[]) {
		System.out.println(ids);
//		String[] idArray = ids.split(",");
//		System.out.println(idArray);
		boolean isSuccess = userService.deleteAll(ids);
		System.out.println(isSuccess);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
		return jsonResult;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update")
	public JSONResult update(User user, String[] roleId) {
		boolean isSuccess = userService.update(user, user.getId(), roleId);

		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "修改成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "修改失败");
		}
		return jsonResult;
	}
	@RequestMapping(value = "/selectId")
	public String selectId(Integer id, Model model) {
		System.out.println(id);
		User user = userService.selectId(id);
		List<Role> list1 = userService.selectRole();
		List<Department> list2 = userService.selectDepartment();
		List<Integer> selectRoleId = userService.selectRoleIdByUser(id);

		for (Integer integer : selectRoleId) {
			for (Role role : list1) {
				if (integer == role.getId()) {
					role.setChecked(true);
				}
			}
		}
		model.addAttribute("user", user);
		model.addAttribute("roleList", list1);
		model.addAttribute("departmentList", list2);
		return "user_update";
	}
	
	@ResponseBody
	@RequestMapping("/insert")
	public JSONResult insert(User user, String[] roleId) {
		boolean isSuccessUser = userService.insert(user, roleId);
		System.out.println(isSuccessUser);

		JSONResult jsonResult = null;
		if (isSuccessUser) {
			jsonResult = JSONResult.success("添加成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "添加失败");
		}
		
		return jsonResult;
	}
	
	@RequestMapping(value = "/selectDepartmentRole")
	public String selectDepartmentRole(Model model) {
		List<Role> list1 = userService.selectRole();
		List<Department> list2 = userService.selectDepartment();
		model.addAttribute("roleList", list1);
		model.addAttribute("departmentList", list2);
		return "user_insert";
	}
	
	@RequestMapping("/getUsersListPage")
	public String getStudentListPage() {
		return "user_list";
	}
}
